package com.liran.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liran.entity.ConsumeInfoEntity;
import com.liran.entity.VipInfoEntity;
import com.liran.mapper.ConsumeInfoMapper;
import com.liran.mapper.VipInfoMapper;
import com.liran.param.ConsumeInfoListParam;
import com.liran.param.VipInfoListParam;
import com.liran.service.ConsumeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author whh
 * @since 2022-07-11
 */
@Service
public class ConsumeInfoServiceImpl extends ServiceImpl<ConsumeInfoMapper, ConsumeInfoEntity> implements ConsumeInfoService {

    @Autowired
    private ConsumeInfoMapper consumeInfoMapper;

    @Autowired
    private VipInfoMapper vipInfoMapper;

    @Override
    public Page<ConsumeInfoEntity> listSearch(ConsumeInfoListParam consumeInfoListParam) {
        QueryWrapper<ConsumeInfoEntity> queryWrapper = new QueryWrapper<>();
        conditionHandle(consumeInfoListParam, queryWrapper);
        Page<ConsumeInfoEntity> page = new Page<>(consumeInfoListParam.getPage(), consumeInfoListParam.getLimit());
        consumeInfoMapper.selectPage(page, queryWrapper);
        setVipName(page);
        return page;
    }

    private void conditionHandle(ConsumeInfoListParam consumeInfoListParam, QueryWrapper<ConsumeInfoEntity> queryWrapper) {
        if (consumeInfoListParam.getVipId() != null) {
            queryWrapper.eq("vip_id", consumeInfoListParam.getVipId());
        }
    }

    private void setVipName(Page<ConsumeInfoEntity> page){
        for (ConsumeInfoEntity consumeInfoEntity : page.getRecords()) {
            if(consumeInfoEntity.getVipId() != null){
                VipInfoEntity vipInfoEntity = vipInfoMapper.selectById(consumeInfoEntity.getVipId());
                consumeInfoEntity.setName(vipInfoEntity.getName());
            }
        }
    }
}
